<img src='' id='GoodsConEdit_Banner' onerror='this.src="../Public/Image/base.png"'  style='width:100%' alt='暂无图片' /> <!--广告位-->
<div style='margin:10px auto;display:flex'>
	<div style='width:100px;text-align:right'>分类选择：</div>
	<div id='GoodsConEdit_TypeSelArea1'>
		<span id='GoodsConEdit_TypeSelArea1_Sub1'></span>　
		<button id='GoodsConEdit_TypeSelArea1_Sub2'>切换</button>
	</div>
	<div style='flex:1;display:none' id='GoodsConEdit_TypeSelArea2'>
		<select id="GoodsConEdit_TypeSel1"></select>
		<select id="GoodsConEdit_TypeSel2"></select>
		<select id="GoodsConEdit_TypeSel3"></select>
	</div>
</div>

<div style='margin:10px auto;display:flex'>
	<div style='width:100px;text-align:right'>商品名称：</div>
	<div style='flex:1'><input type="text" id='GoodsConEdit_Title' style='width:90%' /></div>
</div>
<div style='margin:10px auto;display:flex'>
	<div style='width:100px;;text-align:right'>商品编号：</div>
	<div style='flex:1'><input type="text" id='GoodsConEdit_Number' />(可选)</div>
</div>
<div style='margin:10px auto;display:flex'>
	<div style='width:100px;text-align:right'>商品价格：</div>
	<div style='flex:1'><input type="text" id='GoodsConEdit_Price' /></div>
</div>

<div style='margin:10px auto;display:flex'>
	<div style='width:100px;;text-align:right'>商品详情：</div>
	<div style='flex:1'><textarea id='GoodsConEdit_Info' style='width:90%;height:150px;resize:none' ></textarea></div>
</div>

<div style='margin:10px auto;display:flex'>
	<div style='width:100px;text-align:right'>图片展示：</div>
	<div style='flex:1'>
		<div id='GoodsConEdit_ImgList'></div>
		<div style='position:relative;margin:10px' id='GoodsConEdit_Img_Area'>
			<img src="../Public/Image/file.png" alt="" style='width:40px'>
			<input type="file" id='GoodsConEdit_Img' style='width:40px;height:40px;opacity:0;position:absolute;top:0px;left:0px;' />
		</div>
	</div>
</div>


<div style='text-align:center;margin:20px auto'>
	<button id='GoodsConEdit_SubBtn'>提交</button>
	<button id='GoodsConEdit_BackBtn'>返回</button>
</div>



<script>
	var edit_id = sessionStorage.goods_edit_id;
	var GoodsConEdit_info = '';


	//===== 展示图片相关
	_getAndViewImgList();

	function _getAndViewImgList(){
		_getGoodsImgs(function(imgListData){
			_parseGoodsImgs(imgListData);
		});
	}

	function _getGoodsImgs(fun){
		var data = {
			type:'getGoodsImgs',
			goods_id:edit_id
		};
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			fun(tmp.data);
		});
	}

	function _parseGoodsImgs(imgListData){
		var StoreGoodsEditImgs_ListStr = '';
		for(var i=0;i<imgListData.length;i++){
			StoreGoodsEditImgs_ListStr += "<div style='margin:10px;position:relative;display:inline-block;border:1px solid #ccc;'>";
			StoreGoodsEditImgs_ListStr += "<span style='border:1px solid;padding:0 6px;border-radius:15px;position:absolute;right:-10px;top:-10px;background:#fff' onclick=GoodsConEdit_DelImg("+imgListData[i].id+",'"+imgListData[i].addr+"') >X</span>";
		
			var src = upyun_shop_img+imgListData[i].addr;
			StoreGoodsEditImgs_ListStr += "<img srcs='../Public/Image/base.png' src='"+src+"' style='height:80px' alt='暂无'>";
			
			StoreGoodsEditImgs_ListStr += "<div style='text-align:center;margin:5px 0;font-size:13px;color:blue' onclick=GoodsConEditImgs_ChangeBanner('"+imgListData[i].addr+"')>设置封面</div>";
			StoreGoodsEditImgs_ListStr += "</div>";
		}
		$('#GoodsConEdit_ImgList').html(StoreGoodsEditImgs_ListStr);
	}


	//删除图片
	function GoodsConEdit_DelImg(img_id,img_addr){
		if(!confirm('确认操作?')){
			return;
		}
		var data = {
			type:'goodsImgDel',
			img_id:img_id,
			img_addr:img_addr,
			goods_id:edit_id
		};
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				if(GoodsConEdit_info.banner == img_addr){
					GoodsConEdit_info.banner = '';	//清空商品信息中的banner
					$('#GoodsConEdit_Banner').attr('src',base_img);	//将banner置为默认图片
				}
				A('操作成功！');
				_getAndViewImgList();//重新加载图片列表
			}else{
				A('操作失败！');
			}
		});
	}

	//修改封面图
	function GoodsConEditImgs_ChangeBanner(path){
		$('#GoodsConEdit_Banner').attr('src',upyun_shop_img+path);
		GoodsConEdit_info.banner = path;
	}









	//选择并上传图片
	var GoodsConEdit_Img = document.getElementById('GoodsConEdit_Img');
	GoodsConEdit_Img.onchange=function(){
		Notice('图片上传中......');
		var file = GoodsConEdit_Img.files[0];
		if(CheckFile(file,100*1024) != true){
			Notice('');
			A('js-图片只支持jpg,gif,png格式，且不得大于100k!');return;
		}
		//sessionStorage.removeItem('img_list_'+edit_current_id);	//删除当前id商品对应的图片列表缓存

		var formData = new FormData();
		formData.append('type','goodsImageAdd');
		formData.append('img',file); //添加文件
		formData.append('user',accountInfo.account);
		formData.append('cityCode',cityInfo[0]);
		formData.append('db_host',cityInfo[1]);
		formData.append('db_name',cityInfo[2]);
		formData.append('goods_id',edit_id); //添加信息
		formData.append('store_id',accountInfo.id); //添加信息
		formData.append('token1',sessionStorage.store_token1); //添加信息
		//Ajax_FormData2(formData,after_url,function(res){
		AjaxFile(formData,function(res){
			var tmp = _checkJsonFormat(res);
			switch(tmp.stat){
				case 'err2':A('图片数量超限！');break;
				case 'err3':A('文件上传失败！');break;
				case 'err4':A('数据写入失败！');break;
				case 'err5':A('图片大小超限！');break;
				case 'ok':A('上传成功！');
					_getAndViewImgList();
				break;
			}
			Notice('');
		},_store_getServerAddr()+default_storeRequestPath);

	}




	//点击切换商品类型
	$('#GoodsConEdit_TypeSelArea1_Sub2').click(function(){
		//加载三级商品分类 
		$('#GoodsConEdit_TypeSelArea2').show();
		$('#GoodsConEdit_TypeSelArea1').hide();
		//获取商品分类列表
		_getGoodsTypeListFull(function(GoodsTypes){
			//分类数据填充到元素
			GoodsTypeInit(GoodsTypes,'#GoodsConEdit_TypeSel1','#GoodsConEdit_TypeSel2','#GoodsConEdit_TypeSel3');
		});
	});


	//提交更改
	$('#GoodsConEdit_SubBtn').click(function(){
		if($('#GoodsConEdit_TypeSelArea2').is(':visible')){
			var t1 = $('#GoodsConEdit_TypeSel1').find('option:selected').val();
			var t2 = $('#GoodsConEdit_TypeSel2').find('option:selected').val();
			var t3 = $('#GoodsConEdit_TypeSel3').find('option:selected').val();
			var t1_text = $('#GoodsConEdit_TypeSel1').find('option:selected').text();
			var t2_text = $('#GoodsConEdit_TypeSel2').find('option:selected').text();
			var t3_text = $('#GoodsConEdit_TypeSel3').find('option:selected').text();
			var type_set = t1_text+"--"+t2_text+"--"+t3_text;
			//C(type_set);
		}else{
			var t1 = GoodsConEdit_info.tid1;
			var t2 = GoodsConEdit_info.tid2;
			var t3 = GoodsConEdit_info.tid3;
			var type_set = GoodsConEdit_info.type_set;
		}
		var GoodsTitle = ChangeChar($('#GoodsConEdit_Title').val().trim());
		//Field_Check(/^.{1,50}$/,GoodsTitle,'js商品名1-50个字符!');

		var GoodsNumber = $('#GoodsConEdit_Number').val().trim();
		//Field_Check(/^[0-9]{1,4}$|^$/,GoodsNumber,'js编号为0-4四位数字!');

		var GoodsPrice = $('#GoodsConEdit_Price').val().trim();
		Field_Check(/^[1-9][0-9]{0,8}(\.[0-9]{1,2})?|0\.[0-9]{2}?$/,GoodsPrice,'js价格不合法!');

		var GoodsInfo = ChangeChar($('#GoodsConEdit_Info').val().trim());
		//Field_Check(/^.{1,}$/,GoodsInfo,'js商品详情不得为空!');

		var data = {
			type:'storeGoodsEdit',
			goods_id:edit_id,
			title:GoodsTitle,
			number:GoodsNumber,
			price:GoodsPrice,
			info:GoodsInfo,
			t1:t1,
			t2:t2,
			t3:t3,
			type_set:type_set,
			banner:GoodsConEdit_info.banner
		}
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			switch(tmp.stat){
				case 'err2':A('操作失败!');break;
				case 'err3':A('缺少参数！');break;
				case 'err4':A('商品名1-50个字符!');break;
				case 'err5':A('编号为0-4四位数字!');break;
				case 'err6':A('价格不合法!');break;
				case 'err7':A('商品详情不得为空!');break;
				case 'ok':A('操作成功!');break;
			}
			GCE_BackCon();
			GoodsCon_Init(1);
		},sessionStorage.store_requestAddr);
	});

	GCE_GetGoodsInfo();

	function GCE_GetGoodsInfo(){
		var data = {
			type:'getGoodsInfo',
			goods_id:edit_id
		};
		AjaxStore(data,function(res){
			//C(res);
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				GCE_GetGoodsInfo_Parse(tmp.data);
			}
		});
	}

	function GCE_GetGoodsInfo_Parse(res){
		if(typeof(res) == 'object' && res.id){
			GoodsConEdit_info = res;
			//C(GoodsConEdit_info);
			if(GoodsConEdit_info.banner){
				var GoodsConEdit_Banner = upyun_shop_img+GoodsConEdit_info.banner;
			}else{
				var GoodsConEdit_Banner = base_img;
			}
			$('#GoodsConEdit_Banner').attr('src',GoodsConEdit_Banner);
			$('#GoodsConEdit_Title').val(ChangeChar(GoodsConEdit_info.title,1));
			$('#GoodsConEdit_Number').val(GoodsConEdit_info.number);
			$('#GoodsConEdit_Price').val(GoodsConEdit_info.price);
			$('#GoodsConEdit_Info').val(ChangeChar(GoodsConEdit_info.info,1));
			$('#GoodsConEdit_TypeSelArea1_Sub1').text(GoodsConEdit_info.type_set);
			$('#GoodsConEdit_TypeSel1').val(GoodsConEdit_info.tid1);
		}
	}


	$('#GoodsConEdit_BackBtn').click(function(){
		GCE_BackCon();
	});

	function GCE_BackCon(){
		$('input').val('');
		$('textarea').val('');
		$('#GoodsCon_List').show();
		$('#GoodsCon_AddBtn').show();
		$('#GoodsCon_Edit').hide();
	}

</script>